private BitmapDescriptor bitmapDescriptorFromVector(Context context,
@DrawableRes int vectorDrawableResourceId) {
Drawable background = ContextCompat.getDrawable(context,
vectorDrawableResourceId);
background.setBounds(0, 0, background.getIntrinsicWidth(),
background.getIntrinsicHeight());
// Drawable vectorDrawable = ContextCompat.getDrawable(context,
vectorDrawableResourceId);
// vectorDrawable.setBounds(40, 20, vectorDrawable.getIntrinsicWidth() +
40, vectorDrawable.getIntrinsicHeight() + 20);
Bitmap bitmap = Bitmap.createBitmap(background.getIntrinsicWidth(),
background.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
background.draw(canvas);
// vectorDrawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
按鈕事件
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Intent intent = getIntent();
double lat = intent.getDoubleExtra("lat", 0.0);
double lng = intent.getDoubleExtra("lng", 0.0);
if (lat != 0.0 && lng != 0.0) {
final DialogInterface.OnClickListener listener =
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
// 更新位置資訊
case DialogInterface.BUTTON_POSITIVE:
// 連線到Google API用戶端
if (!googleApiClient.isConnected()) {
googleApiClient.connect();
}
break;
// 清除位置資訊
case DialogInterface.BUTTON_NEUTRAL:
Intent result = new Intent();
result.putExtra("lat", 0);
result.putExtra("lng", 0);
setResult(Activity.RESULT_OK, result);
finish();
break;
// 取消
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
if (marker.equals(itemMarker)) {
AlertDialog.Builder ab = new
AlertDialog.Builder(MapsActivity.this);
ab.setTitle(R.string.title_update_location)
.setMessage(R.string.message_update_location)
.setCancelable(true);
ab.setPositiveButton(R.string.update, listener);
ab.setNeutralButton(R.string.clear, listener);
ab.setNegativeButton(android.R.string.cancel, listener);
ab.show();
}
}
// 如果是目前位置標記
else if (marker.equals(currentMarker)) {
AlertDialog.Builder ab = new
AlertDialog.Builder(MapsActivity.this);
ab.setTitle(R.string.title_current_location)
.setMessage(R.string.message_current_location)
.setCancelable(true);
ab.setPositiveButton(android.R.string.ok, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent result = new Intent();
result.putExtra("lat", currentLocation.getLatitude());
result.putExtra("lng",
currentLocation.getLongitude());
setResult(Activity.RESULT_OK, result);
finish();
}
});
ab.setNegativeButton(android.R.string.cancel, null);
ab.show();
return true;
}
return false;
}
});